package com.zsck.pos.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.appinterface.update.AppJson;
import com.appinterface.update.UpdateHelper;
import com.appinterface.update.UpdateListener;
import com.chinaums.commoncheck.R;
import com.zsck.pos.base.BaseTitleActivity;
import com.zsck.pos.bean.Project;
import com.zsck.pos.bean.UserInfo;
import com.zsck.pos.net.MyObserver;
import com.zsck.pos.net.NetConfig;
import com.zsck.pos.net.RetrofitCilent;
import com.zsck.pos.utils.LogUtil;
import com.zsck.pos.utils.SharePreferenceUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.zsck.net.Utils.RxHelper.observableIO2Main;

public class MainActivity extends BaseTitleActivity {


    /*名字*/
    @BindView(R.id.tv_name)
    TextView mTvName;
    /*帐号*/
    @BindView(R.id.tv_account)
    TextView mTvAccount;
    /*收款*/
    @BindView(R.id.btn_collection)
    ImageView mBtnCollection;
    /*记录*/
    @BindView(R.id.btn_records)
    ImageView mBtnRecords;
    /*退出登录*/
    @BindView(R.id.logout)
    TextView mTvLogout;
    @BindView(R.id.btn_multi)
    ImageView btnMulti;

    @Override
    public int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    public String setTitle() {
        return null;
    }

    @Override
    public Activity setActivity() {
        return this;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initEvents();
        initData();
        /*获取设备信息*/

//        UpdateHelper.checkUpdate(this, "com.ums.sz.pos", updateListener);


    }

    private UpdateListener updateListener = new UpdateListener() {

        @Override
        public void onCheckUpdateResult(int i, AppJson appJson) {
            super.onCheckUpdateResult(i, appJson);
            if (i == 1) {//有版本更新
                UpdateHelper.download(MainActivity.this, "", appJson.getPackName(), true, false, new UpdateListener() {
                    @Override
                    public void onProgress(int i) {
                        super.onProgress(i);
                    }

                    @Override
                    public void onStateChanged(int i) {
                        super.onStateChanged(i);
                    }
                });
            }
        }
    };

    private void initEvents() {
        mBtnCollection.setOnClickListener(this);
        mBtnRecords.setOnClickListener(this);
        mTvLogout.setOnClickListener(this);
        btnMulti.setOnClickListener(this);
    }

    private void initData() {
        mToolbar.setVisibility(View.GONE);
        UserInfo userInfo = NetConfig.userInfo;
        mTvName.setText(userInfo.getName());
        mTvAccount.setText(userInfo.getAccountId());


    }

    @Override
    protected void onResume() {
        super.onResume();
        getProjectAppSelectList();


    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_multi:
                Intent intentMu = new Intent();
                ComponentName componentNameMu = new ComponentName("com.ums.sz.pos", "com.zsck.pos.activity.MultiBillsConditionActivity");
                intentMu.setComponent(componentNameMu);
                startActivity(intentMu);
                break;
            case R.id.btn_collection:
                Intent intent = new Intent();
                ComponentName componentName = new ComponentName("com.ums.sz.pos", "com.zsck.pos.activity.BillsActivity");
                intent.setComponent(componentName);
                startActivity(intent);
                break;
            case R.id.btn_records:
                Intent intent2 = new Intent();
                ComponentName componentName2 = new ComponentName("com.ums.sz.pos", "com.zsck.pos.activity.RecordsActivity");
                intent2.setComponent(componentName2);
                startActivity(intent2);
                break;
            case R.id.logout:
                NetConfig.userInfo = null;
                SharePreferenceUtils.clear(this);
                Intent intent3 = new Intent();
                ComponentName componentName3 = new ComponentName("com.ums.sz.pos", "com.zsck.pos.activity.LoginActivity");
                intent3.setComponent(componentName3);
                startActivity(intent3);
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            //return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    private void getProjectAppSelectList() {
        String possn = SharePreferenceUtils.getString(this, "POSSN");
        Map<String, Object> map = new HashMap<>();
        if (!TextUtils.isEmpty(possn)) {
            map.put("posId", possn);
        }
        MyObserver<List<Project>> observer = new MyObserver<List<Project>>(this) {
            @Override
            public void onSuccess(List<Project> result) {
                NetConfig.projects = result;
                LogUtil.e("projects", result.toString());
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                LogUtil.e("test", e.getMessage());
            }

            @Override
            public void reRequest() {
                getProjectAppSelectList();
            }
        };
       /* RetrofitCilent.getApiService().getProjectAppSelectList(map).
                compose(observableIO2Main(this)).subscribe(observer);*/

        RetrofitCilent.getApiService().queryProject(map).
                compose(observableIO2Main(this)).subscribe(observer);
    }

}
